# coding: utf-8
from random import randint

from PySide6.QtCore import Property, Qt
from PySide6.QtGui import QResizeEvent
from PySide6.QtWidgets import QApplication, QPushButton, QLabel
from qfluentwidgets import PopUpAniStackedWidget


class Direction:
    LeftOrRight = 0
    TopOrBottom = 1


class SlideAniStackedWidget(PopUpAniStackedWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.__direction = Direction.LeftOrRight

    def setDirection(self, direction: Direction):
        self.__direction = direction

    def getDirection(self) -> Direction:
        return self.__direction

    def next(self):
        """
        下一页
        :return:
        """
        curr_index = self.currentIndex()
        if curr_index == self.count() - 1:
            return
        self.setCurrentIndex(curr_index + 1)

    def prev(self):
        """
        上一页
        :return:
        """
        curr_index = self.currentIndex()
        if curr_index == 0:
            return
        self.setCurrentIndex(curr_index - 1)

    direction = Property(Direction, getDirection, setDirection)


class MyStackedWidget(SlideAniStackedWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.next_btn = QPushButton("Next", self)
        self.prev_btn = QPushButton("Prev", self)

        self.next_btn.clicked.connect(self.next)
        self.prev_btn.clicked.connect(self.prev)

        for i in range(5):
            label = QLabel(f"Page {i + 1}", self)
            label.setStyleSheet(
                f'QLabel {{font-size: 30px; color: white; background-color: rgba({randint(0, 255)}, {randint(0, 255)}, {randint(0, 255)}, 100); border-radius: 10px; padding: 20px;}}')
            label.setAlignment(Qt.AlignmentFlag.AlignCenter)
            self.addWidget(label, deltaY=self.height())

        self.next_btn.raise_()
        self.prev_btn.raise_()

    def resizeEvent(self, event: QResizeEvent):
        self.next_btn.move(self.width() - self.next_btn.width() - 10, (self.height() - self.prev_btn.height()) // 2)
        self.prev_btn.move(10, (self.height() - self.prev_btn.height()) // 2)

    def setCurrentIndex(self, index: int):
        super().setCurrentIndex(index)
        self.next_btn.raise_()
        self.prev_btn.raise_()


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    demo = MyStackedWidget()
    demo.resize(1000, 800)
    demo.show()
    sys.exit(app.exec())
